package com.xinghai.web.controller.dingtalk;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xinghai.common.annotation.Log;
import com.xinghai.common.core.controller.BaseController;
import com.xinghai.common.core.domain.AjaxResult;
import com.xinghai.common.core.domain.entity.SysDept;
import com.xinghai.common.core.page.TableDataInfo;
import com.xinghai.common.enums.BusinessType;
import com.xinghai.common.exception.ServiceException;
import com.xinghai.common.utils.poi.ExcelUtil;
import com.xinghai.system.domain.XhCarTypeInfo;
import com.xinghai.system.service.ISysDeptService;
import com.xinghai.system.service.IXhCarTypeInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Optional;

/**
 * 车型信息Controller
 * 
 * @author xinghai
 * @date 2024-07-23
 */
@RestController
@RequestMapping("/car/type/info")
public class XhCarTypeInfoController extends BaseController
{
    @Autowired
    private IXhCarTypeInfoService xhCarTypeInfoService;
    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询车型信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(XhCarTypeInfo xhCarTypeInfo)
    {
        startPage();
        List<XhCarTypeInfo> list = xhCarTypeInfoService.list(xhCarTypeInfo);
        return getDataTable(list);
    }

    /**
     * 导出车型信息列表
     */
    @Log(title = "车型信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, XhCarTypeInfo xhCarTypeInfo)
    {
        List<XhCarTypeInfo> list = xhCarTypeInfoService.list(xhCarTypeInfo);
        if (!list.isEmpty()) {
            //查询物流公司
            List<SysDept> deptList = sysDeptService.selectDeptList(new SysDept());
            list.forEach(item -> {
                Optional.ofNullable(deptList.stream().filter(f->item.getDeptId().equals(f.getDeptId())).findFirst().orElse(null)).ifPresent(val->{
                    item.setDeptName(val.getDeptName());
                });
            });
        }
        ExcelUtil<XhCarTypeInfo> util = new ExcelUtil<XhCarTypeInfo>(XhCarTypeInfo.class);
        util.exportExcel(response, list, "车型信息数据");
    }

    /**
     * 获取车型信息详细信息
     */
    @GetMapping("/getInfo")
    public AjaxResult getInfo(Long id)
    {
        return success(xhCarTypeInfoService.getInfo(id));
    }

    /**
     * 新增车型信息
     */
    @Log(title = "车型信息", businessType = BusinessType.INSERT)
    @PostMapping("/insert")
    public AjaxResult add(@RequestBody XhCarTypeInfo xhCarTypeInfo) throws Exception {
        return toAjax(xhCarTypeInfoService.insert(xhCarTypeInfo));
    }

    /**
     * 修改车型信息
     */
    @Log(title = "车型信息", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody XhCarTypeInfo xhCarTypeInfo) throws Exception {
        return toAjax(xhCarTypeInfoService.update(xhCarTypeInfo));
    }

    /**
     * 删除车型信息
     */
    @Log(title = "车型信息", businessType = BusinessType.DELETE)
	@PostMapping("/delete")
    public AjaxResult delete(Long id) throws Exception {
        return toAjax(xhCarTypeInfoService.delete(id));
    }


    /**
     * 批量删除车型信息
     */
    @Log(title = "车型信息", businessType = BusinessType.DELETE)
    @PostMapping("/deleteBatch")
    public AjaxResult deleteBatch(Long[] ids) throws Exception {
        return success(xhCarTypeInfoService.deleteBatch(ids));
    }

    /**
     * 导入车型信息模板
     */
    @PreAuthorize("@ss.hasPermi('system:info:import')")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<XhCarTypeInfo> util = new ExcelUtil<XhCarTypeInfo>(XhCarTypeInfo.class);
        util.importTemplateExcel(response, "车型信息数据模板");
    }


    /**
     * 导入车型信息
     */
    @Log(title = "车型信息管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file)
    {
        ExcelUtil<XhCarTypeInfo> util = new ExcelUtil<XhCarTypeInfo>(XhCarTypeInfo.class);
        List<XhCarTypeInfo> xhCarTypeInfoList = null;
        try {
            xhCarTypeInfoList = util.importExcel(file.getInputStream());
        } catch (IOException e) {
            throw new ServiceException("导入数据失败");
        }
        return success(xhCarTypeInfoService.importData(xhCarTypeInfoList));
    }

    /**
     * 车辆信息页面查询车型信息列表
     */
    @GetMapping("/car/type/list")
    public AjaxResult carTypeList(XhCarTypeInfo xhCarTypeInfo)
    {
        List<XhCarTypeInfo> list = xhCarTypeInfoService.carTypeList(xhCarTypeInfo);
        return success(list);
    }
}
